package com.example.demo.controller;

import com.example.demo.model.MessageInfo;
import com.example.demo.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:DELL
 * Date:2025-05-07
 * Time:17:14
 */
@RequestMapping("/message")
@RestController
public class MessageController {
    //private List<MessageInfo> messageInfos=new ArrayList<>();
    @Autowired
    private MessageService messageService;
    @RequestMapping("/publish")
    public  Boolean publish(MessageInfo messageInfo){
        System.out.println("接收参数，messageInfo:"+messageInfo);
        //1.参数校验
        //2.存储数据，暂时存在内存中
        if(!StringUtils.hasLength(messageInfo.getFrom())
                ||!StringUtils.hasLength(messageInfo.getTo())
                ||!StringUtils.hasLength(messageInfo.getMessage())){
            return false;
        }
        //messageInfos.add(messageInfo);
        Integer result = messageService.publicMessage(messageInfo);
        if(result>0){
            return true;
        }
        return false;
    }
    /*获取留言信息*/
    @RequestMapping("/getList")
    public List<MessageInfo> getList(){

        return messageService.getMessageList();
    }
}
